<?php

namespace App\controller;

use App\model\GameLog;
use App\model\Role;
use app\model\User;
use support\Common;
use support\Db;
use support\Redis;
use support\Request;
use support\Response;

class RankController extends CommonController
{
    /**
     * 等级排行
     * @param Request $request
     * @return Response
     */
    public function list(Request $request)
    {
        $data = $request->postData;
        // $job_id = $data['job_id'];//1-战士 2-道士 3-法
        $type = $data['type'];//1-等级 2-术士 3-剑士 4-方士 5-任务榜
        $uuid = $data['uuid'];
        $role_id = $data['role_id'];

        // print_r($data);

        // $out_data = Redis::get('rank_cache' . $type . $uuid . $role_id);
        // if ($out_data) {
        //     $out_data = json_decode($out_data, true);
        //     return outJson(0, 'ok', $out_data);
        // }

        $cond_where = [];
        $order_by = 'level desc,exp desc';
        if ($type == 2) {
            $order_by = 'fight_cap desc';
            $cond_where['job_id'] = 3;
        } elseif ($type == 3) {
            $order_by = 'fight_cap desc';
            $cond_where['job_id'] = 1;
        } elseif ($type == 4) {
            $order_by = 'fight_cap desc';
            $cond_where['job_id'] = 2;
        } elseif ($type == 5) {
            // $order_by = 'fight_cap desc';
            // $cond_where['job_id'] = 4;
        }

        $cond_not_in = [
            '1084f571be084706163ddd5f67a136a44221bba1990aec4cc84b83cb159a7bec',
            'a2f392cf4e8a859b982963aeee8cc8ac3ba7e0c1dbf305d36d37d13be8057d37',
            'b01de84f91ba3cd916de806fdcc7a3ec78077e72d2b9e292b06a46591b1f3169',
            'b01de84f91ba3cd916de806fdcc7a3ec78077e72d2b9e292b06a46591b1f3169',
        ];

        $rank_all = Role::query()->where($cond_where)->whereNotIn('uuid', $cond_not_in)->orderByRaw(Db::raw($order_by))->limit(200)->get();
        $rank_all = $rank_all ? $rank_all->toArray() : [];
        $_rank_all = $rank_all;
        $rank_datas = array_splice($rank_all, 0, 20);

        $uuids = array_column($rank_datas, 'uuid');
        $uuids[] = $uuid;

        $users = User::query()->whereIn('uuid', $uuids)->get();
        $users = $users ? $users->toArray() : [];
        $users = array_column($users, null, 'uuid');

        if (in_array($type, [2, 3, 4])) {
            $role_info = (new Role())->getRoleInfoByUuidRoleId($uuid, $role_id, $cond_where['job_id']);
        } elseif ($type == 1) {
            $role_info = Role::query()->where('uuid', $uuid)->where('role_id', $role_id)->orderByDesc('level')->first();
            $role_info = $role_info ? $role_info->toArray() : [];
        }

        // Common::writeLog([$role_info, $uuid, $role_id, '看日志'], __FILE__, __FUNCTION__, __LINE__);

        $list = [];
        $rank = 1;
        foreach ($rank_datas as $item) {
            $list[] = [
                'rank' => $rank,
                'level' => $item['level'],
                'icon' => $users[$item['uuid']]['icon'] ?? 'http://up5.nosdn.127.net/4/dz/res/player/header/static/normal/icon1.jpg',
                'nickname' => $users[$item['uuid']]['nickname'] ?? '测试',
                'fight_cap' => $item['fight_cap'],
            ];
            $rank++;
        }
        // Common::writeLog($rank_all, __FILE__, __FUNCTION__, __LINE__);
        $_rank = 201;
        foreach ($_rank_all as $key => $item) {
            if ($item['uuid'] == $uuid && $item['role_id'] == $role_id) {
                $_rank = $key + 1;
                break;
            }
        };

        $my_rank['rank'] = $_rank;
        $my_rank['level'] = $role_info['level'] ?? 0;
        $my_rank['icon'] = $users[$item['uuid']]['icon'] ?? 'http://up5.nosdn.127.net/4/dz/res/player/header/static/normal/icon1.jpg';
        $my_rank['nickname'] = $users[$item['uuid']]['nickname'] ?? '测试';
        $my_rank['fight_cap'] = $role_info['fight_cap'] ?? 0;

        $out_data['list'] = $list;
        $out_data['my_rank'] = $my_rank;

        // Redis::setEx('rank_cache' . $type . $uuid . $role_id, 30 * 60, json_encode($out_data));
        return outJson(0, 'ok', $out_data);
    }

}
